<template>
	<view class="about">
	
		<!-- 导航 -->
		<u-navbar back-text="搜索" :back-text-style="textStyle" back-icon-color="#000000" style="font-weight: bold;font-family: PingFang SC;"></u-navbar>
		
		<view     style="width: 93%;margin: auto;height: 66rpx;border: 1rpx solid #FB6112;border-radius: 33rpx;position: relative;">
			<view style="width: 100%;display: flex;align-items: center;">
				<image style="width: 26rpx;height: 32rpx;margin: 0rpx 10rpx 0rpx 15rpx;" src="../../../static/gx_img/dw_icon.png"></image>
				<view  @tap.stop="jumpToSelectCity" style="width: 120rpx;font-size: 26rpx;line-height: 66rpx; color: #000000;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;"
				>
				{{siteName||'郑州市'}}	
				</view>
				<view style="height:34rpx ;width: 1rpx;background-color:#D2D2D2 ;margin-right: 18rpx;"></view>
				<u-icon name="search" color="#D2D2D2" size="28"></u-icon>
				<view style="width: 350rpx; font-size: 26rpx;color: #333333;margin-left: 10rpx;">
					<input type="text" :adjust-position="true" :placeholder="defaultKeyword" v-model="keyword" @confirm="doSearch(keyword)"
				 placeholder-class="placeholder-class" confirm-type="search" placeholder-style="font-size: 26rpx;color: #E4E4E4;" />
				</view>
			</view>
			<view @tap="doSearch(keyword)" style="top: 2rpx;right: 2rpx;color: white; position: absolute; border-radius: 32rpx; text-align: center;line-height: 58rpx; width:106rpx;height: 58rpx;background: linear-gradient(180deg, #FB5A0B 0%, #FFAA00 100%);font-size: 26rpx;">
				搜索
			</view>	
		</view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view style="color: #000000;font-size: 30rpx;">热门搜索</view>
						<!-- <view>
							<image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
						</view> -->
					</view>
					<view class="keyword" v-if="forbid==''">
						<view style="background-color: #FFEADC;border-radius: 10rpx;color: #FC5C02;" v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword.searchContent)" :key="index">
						<view style="display: flex;align-items: center;">
							<image style="width: 24rpx;height: 28rpx;margin-right:10rpx ;" src="../../../static/gx_img/hot_icon.png"></image>
							<view style="font-size: 26rpx;">{{keyword.searchContent}}</view>
						</view>
						
						</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view style="color: #000000;font-size: 30rpx;">历史搜索</view>
					<!-- 	<view>
							<image @tap="oldDelete" src="/static/HM-search/delete.png"></image>
						</view> -->
					</view>
					<view class="keyword">
						<view style="background-color: #F3F4F8;border-radius: 10rpx;color: #000000;font-size: 26rpx;"  v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
					</view>
				</view>
		
			</scroll-view>
		</view>
	
</template>

<script>
//引用mSearch组件，如不需要删除即可
	import mSearch from '@/components/mehaotian-search-revision.vue';
	export default {
		data() {
			return {
				defaultKeyword: "请输入关键词",
				keyword:'',
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false,
				siteName:'',
				token:''
			}
		},
		onLoad() {
			this.init();
			this.getLocationInfo()
			this.token = uni.getStorageSync('token');
		},
		components: {
			//引用mSearch组件，如不需要删除即可
			mSearch
		},
		methods: {
			//跳转选择城市
			jumpToSelectCity: function() {
				if (!this.token) {
					this.islogin();
					return;
				} else {
					uni.navigateTo({
						url: '/pageageA/home/selectCity/selectCity?cityName=' + this.siteName
					});
				}
			},
			getLocationInfo() {
				var _this = this;
				uni.getStorage({
					key: 'siteName',
					success: res => {
						_this.siteName = res.data;
					},
					fail: resd => {
						//2. 获取地理位置
						uni.getLocation({
							type: 'wgs84',
							success(res) {
								// console.log("你当前经纬度是：");
								console.log(res);
								let latitude, longitude;
								latitude = res.latitude.toString();
								longitude = res.longitude.toString();
								var myAmapFun = new amap.AMapWX({
									key: _this.http.mapVxKey
								});
								myAmapFun.getRegeo({
									location: '' + longitude + ',' + latitude + '', //location的格式为'经度,纬度'
									success: function(e) {
										console.log("====")
										_this.siteName = e[0].regeocodeData.addressComponent.city; //城市
										console.log(_this.siteName)
										uni.setStorage({
											key: 'siteName',
											data: e[0].regeocodeData.addressComponent.city
										});
									},
									fail: function(info) {
										console.log("------")
										console.log('失败');
									}
								});
							},
							fail(resd){
								console.log(resd);
							}
						});
					}
				});
			},
			// 登录判断
			islogin() {
				uni.showModal({
					title: '登录提示',
					content: '该功能需要登录后使用。',
					confirmText: '去登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				return;
			},
			// 热门
			serviceList(item) {
			
					switch (item.chargeType) {
						case 1:
							uni.navigateTo({
								url: '/pageageA/service/serviceDetail?titleName=' + item.name + '&redirectId=' + item.businessId + '&chargeType=' + item.chargeType
							});
							break;
						case 2:
							uni.navigateTo({
								url: '/pageageA/service/serviceDetail?titleName=' + item.name + '&redirectId=' + item.businessId + '&chargeType=' + item.chargeType
							});
							break;
						case 3:
							uni.navigateTo({
								url: '/pageageA/service/serviceDetail?titleName=' + item.name + '&redirectId=' + item.businessId + '&chargeType=' + item.chargeType
							});
							break;
						case 4:
							uni.navigateTo({
								url: '../../pageageA/home/almightyNanny/almightyNanny?titleName=' + item.name + '&redirectId=' + item.businessId
							});
							break;
						default:
							break;
					}
				
			},
			//热门搜索
			getHotSearchList: function() {
				let opts = {
					way: true, //true JSON请求；false 表单请求
					method: 'post',
					url: 'search/getHotSearchList'
				};
				let data = {};
				this.http.httpTokenRequest(opts, data).then(res => {
					//打印请求返回的数据
				
					if (res.code == '200') {
						this.hotKeywordList = res.data;
					} else {
						this.$refs.uToast.show({
							title: res.message,
							type: 'error'
						});
					}
				});
			},
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword();

			},
			blur(){
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "请输入关键词";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				
				
				let opts = {
					way: true, //true JSON请求；false 表单请求
					method: 'post',
					url: 'search/getHotSearchList'
				};
				let data = {};
				this.http.httpTokenRequest(opts, data).then(res => {
					//打印请求返回的数据
				
					if (res.code == '200') {
						this.hotKeywordList = res.data;
					} else {
						this.$refs.uToast.show({
							title: res.message,
							type: 'error'
						});
					}
				});
			}, 
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				var keyword = event.detail?event.detail.value:event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				//以下示例截取淘宝的关键字，请替换成你的接口
				uni.request({
					url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
					success: (res) => {
						this.keywordList = [];
						this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
						
					}
				});
			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(keyword) {
				console.log(this.keyword);
				if(keyword.length>0){
					// keyword = keyword===false?this.keyword:keyword;
					this.keyword = keyword;
					
					this.saveKeyword(keyword); //保存为历史
					uni.navigateTo({
						url:'./search_result?keyword='+keyword
					})
					
					
				}else{
					uni.showToast({
						title: '请输入关键词',
						icon: 'none',
						duration: 2000
					});
				}
				 
				// uni.showToast({
				// 	title: keyword,
				// 	icon: 'none',
				// 	duration: 2000
				// });
				//以下是示例跳转淘宝搜索，可自己实现搜索逻辑
				/*
				//#ifdef APP-PLUS
				plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
				//#endif
				//#ifdef H5
				window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
				//#endif
				*/
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style>
	view{display:block;}
	.search-box {width:95%;background-color:rgb(242,242,242);padding:15upx 2.5%;display:flex;justify-content:space-between;position:sticky;top: 0;}
	.search-box .mSearch-input-box{width: 100%;}
	.search-box .input-box {width:85%;flex-shrink:1;display:flex;justify-content:center;align-items:center;}
	.search-box .search-btn {width:15%;margin:0 0 0 2%;display:flex;justify-content:center;align-items:center;flex-shrink:0;font-size:28upx;color:#fff;background:linear-gradient(to right,#ff9801,#ff570a);border-radius:60upx;}
	.search-box .input-box>input {width:100%;height:60upx;font-size:32upx;border:0;border-radius:60upx;-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:0 3%;margin:0;background-color:#ffffff;}
	.placeholder-class {color:#9e9e9e;}
	.search-keyword {width:100%;background-color:rgb(242,242,242);}
	.keyword-list-box {height:calc(100vh - 110upx);padding-top:10upx;border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-entry-tap {background-color:#eee;}
	.keyword-entry {width:94%;height:80upx;margin:0 3%;font-size:30upx;color:#333;display:flex;justify-content:space-between;align-items:center;border-bottom:solid 1upx #e7e7e7;}
	.keyword-entry image {width:60upx;height:60upx;}
	.keyword-entry .keyword-text,.keyword-entry .keyword-img {height:80upx;display:flex;align-items:center;}
	.keyword-entry .keyword-text {width:90%;}
	.keyword-entry .keyword-img {width:10%;justify-content:center;}
	.keyword-box {height:calc(100vh - 110upx);border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-box .keyword-block {padding:10upx 0;}
	.keyword-box .keyword-block .keyword-list-header {width:94%;padding:10upx 3%;font-size:27upx;color:#333;display:flex;justify-content:space-between;}
	.keyword-box .keyword-block .keyword-list-header image {width:40upx;height:40upx;}
	.keyword-box .keyword-block .keyword {width:94%;padding:3px 3%;display:flex;flex-flow:wrap;justify-content:flex-start;}
	.keyword-box .keyword-block .hide-hot-tis {display:flex;justify-content:center;font-size:28upx;color:#6b6b6b;}
	.keyword-box .keyword-block .keyword>view {display:flex;justify-content:center;align-items:center;border-radius:60upx;padding:0 15upx;margin:10upx 20upx 10upx 0;height:60upx;font-size:26upx;background-color:rgb(242,242,242);color:#6b6b6b;}
</style>

